package me.xplabs.engine.display 
{
	import flash.display.BitmapData;
	import flash.filters.BitmapFilter;
	import me.xplabs.build.BuildDisplay;
	import me.xplabs.event.MMouseEvent;
	import me.xplabs.fight.layer.BuildLayer;
	import me.xplabs.Globals;
	import me.xplabs.interfaces.IBaseDisplay;
	/**
	 * 位图渲染的容器，类似Flash的DisplayObjectContainer
	 * ...
	 * @author xiaohan
	 */
	public class BaseDisplayContainer extends BaseDisplay 
	{
		protected var _childs:Vector.<BaseDisplay>;
		protected var _numChildren:uint;
		public var mouseChild:Boolean;
		protected var _hasEvent:Boolean;
		protected var _eventType:String;
		protected var _eventList:Vector.<BaseDisplay>;
		protected var _eventDisplay:BaseDisplay;
		public var isSort:Boolean;
		private var _curFrame:int;
		
		private var _clickEvent:Boolean;
		public function BaseDisplayContainer() 
		{
			_childs = new Vector.<BaseDisplay>();
			_eventList = new Vector.<BaseDisplay>();
			mouseChild = true;
			_eventType = "";
			isSort = false;
		}
		
		protected function sort(p1:BaseDisplay, p2:BaseDisplay):int
		{
			return 0;
		}
		
		override public function update(container:BitmapData):void 
		{
			stage = container;
			if (data) 
			{
				_point.x = localX;
				_point.y = localY;
				_rect = data.rect;
				trim();
				if (isRender)
				{
					if (_filter)
					{
						_filterBitmapData = BitmapData(data).clone();
						_filterBitmapData.applyFilter(data, data.rect, _zeroPoint, _filter);
						container.copyPixels(_filterBitmapData, _rect, _point, null, null , true);
					}else if(_colorTransform) 
					{
						_transformBitmapData = BitmapData(data).clone();
						_transformBitmapData.colorTransform(data.rect, _colorTransform);
					}else
					{
						container.copyPixels(data, _rect, _point, null, null , true);
					}
				}
			}
			
			if (isSort) _childs.sort(sort);
			_eventList.length = 0;
			for (var i:int = 0; i < _numChildren; i++) 
			{
				_childs[i].mouseX = mouseX;
				_childs[i].mouseY = mouseY;
				_childs[i].update(container);
			}
		}

		override public function checkRollEvent(isEvent:Boolean = false):void
		{
			if (mouseChild)
			{
				_eventDisplay = null;
				_hasEvent = false;
				for (var i:int = _numChildren - 1; i >= 0; i--)
				{
					_childs[i].checkRollEvent(_hasEvent);
					if (!_hasEvent)
					{
						_eventDisplay = _childs[i].dispatchRollEvent(_hasEvent);
						_hasEvent = (_eventDisplay != null);
					}else
					{
						_childs[i].dispatchRollEvent(_hasEvent);
					}
				}
			}
		}

		override public function checkMouseEvent(p_type:String = ""):Boolean
		{
			if (mouseChild) 
			{
				for (var i:int = _numChildren - 1; i >= 0; i--)
				{
					if(_childs[i].checkMouseEvent(p_type)) return true
					if (_childs[i].checkMouse(p_type)) return true;
				}
			}
			return false;
		}
		override public function applyFilter(filter:BitmapFilter = null, render:Boolean = true):void 
		{
			for (var i:int = _numChildren - 1; i >= 0; i--)
			{
				_childs[i].applyFilter(filter, render);
			}
			
			_filter = filter
			if (!_filter)
			{
				if (_filterBitmapData)
				{
					_filterBitmapData.dispose();
					_filterBitmapData = null;
				}
			};
		}
		public function addChild(child:BaseDisplay):BaseDisplay
		{
			if (child.parent != this)
			{
				child.parent = this;
				_childs.push(child);
				_numChildren++;
			}else
			{
				setChildIndex(child, _numChildren - 1);
			}
			return child;
		}
		public function removeChild(child:BaseDisplay):BaseDisplay
		{
			if (child.parent != this)
			{
				throw new Error("只能移除显示对象的子类！");
				return;
			}
			for (var i:int = 0; i < _numChildren; i++) 
			{
				if (child == _childs[i])
				{
					_childs.splice(i, 1);
					child.parent = null;
					_numChildren--;
					return null;
				}
			}
			return child;
		}
		public function addChildAt(child:BaseDisplay, index:int):BaseDisplay
		{
			if (child.parent != this)
			{
				_childs.splice(index, 0, child);
				child.parent = this;
				_numChildren++;
			}else
			{
				setChildIndex(child, index);
			}
			return child;
		}
		public function removeChildAt(index:int):BaseDisplay
		{
			if (index<0||index>_numChildren-1)
			{
				return _childs.splice(index, 1)[0];
			}
			return null;
		}
		
		public function swapChildren(child1:BaseDisplay, child2:BaseDisplay):void
		{
			
		}
		
		public function contains(child:BaseDisplay):Boolean
		{
			return false;
		}
		public function getChildAt(index:int):BaseDisplay
		{
			if (index<0||index>_numChildren)
			{
				return _childs[index];
			}
			return null;
		}
		public function getChildByName(name:String):BaseDisplay
		{
			return null;
		}
		public function setChildIndex(child:BaseDisplay, index:int):void
		{
			if (child.parent != this)
			{
				//throw new Error("只能设置显示对象的子类！");
				return;
			}
			for (var i:int = 0; i < _numChildren; i++) 
			{
				if (child == _childs[i])
				{
					_childs.splice(i, 1);
					_childs.splice(index, 0, child);
					break;
				}
			}
		}
		public function swapChildrenAt(index1:int, index2:int):void
		{
			if (index1 < 0 || index2 < 0 || index1 > _numChildren - 1 || index2 > _numChildren - 1)
			{
				//throw new Error("索引值超出范围！");
				return;
			}
			var temp:BaseDisplay = _childs[index1];
			_childs[index1] = _childs[index2];
			_childs[index2] = temp;
		}
		public function childsNameToString():void
		{
			for (var i:int = 0; i < _numChildren; i++) trace(_childs[i].name);
		}
		override public function trim():void 
		{
		}
	}
}